/*
 * Helstius--Basic chemistry application
 * This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 * Copyright 2012 Jaked122
 */
package org.helstius.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class Utility {
	public static boolean contains(int[] array, int n) {
		for (int i : array)
			if (i == n)
				return true;
		return false;
	}

	public static int[] factor(int n) {
		List<Integer> a = new ArrayList<Integer>();
		for (int i = 2; i <= n / i; i++) {
			while (n % i == 0) {
				a.add(i);
				n /= i;
			}
		}
		if (n > 1)
			a.add(n);
		int[] b = new int[a.size()];
		for (int c = 0; c < a.size(); c++)
			b[c] = a.get(c).intValue();
		return b;
	}

	public static int GCD(Collection<Integer> args) {
		int candidate = 1;
		int end = min(args);
		// valid at 1, always.
		boolean[] valid = new boolean[end];
		for (int i = 0; i < end; i++)
			valid[i] = true;
		for (int i = 1; i < end; i++)
			for (int c : args)
				if (c % i != 0)
					valid[i] = false;
		for (int i = end - 1; i >= 0; i--) {
			if (valid[i]) {
				candidate = i + 1;
				break;
			}
		}
		return candidate;

	}

	public static int GCD(int a, int b) {
		int[] c = factor(a);
		int[] d = factor(b);
		int largest = 1;
		for (int i : c) {
			if (contains(d, i))
				largest = Math.max(i, largest);
		}
		return largest;
	}

	public static int GCD(int[] args) {
		int candidate = 1;
		int end = min(args) / 2;
		// valid at 1, always.
		boolean[] valid = new boolean[end];
		for (int i = 0; i < end; i++)
			valid[i] = true;
		for (int i = 1; i <= end; i++)
			for (int c : args)
				if (c % i != 0)
					valid[i] = false;
		for (int i = end - 1; i >= 0; i--) {
			if (valid[i]) {
				candidate = i;
				break;
			}
		}
		return candidate;

	}

	public static int getCountofInstance(int[] arr, int goal) {
		int i = 0;
		for (int c : arr)
			if (c == goal)
				i++;
		return i;
	}

	public static int max(Collection<Integer> e) {
		int a = 0;
		for (int i : e)
			a = Math.max(i, a);
		return a;
	}

	/**
	 * 
	 * @param e
	 *            An array of integers to seek through
	 * @return The largest integer in the set.
	 */
	public static int max(int[] e) {
		int a = 0;
		for (int i : e)
			a = Math.max(i, a);
		return a;
	}

	public static int min(Collection<Integer> i) {
		int largest = max(i);
		for (int c : i)
			if (largest > c)
				largest = c;
		return largest;
	}

	public static int min(int[] i) {
		int largest = max(i);
		for (int c : i)
			if (largest > c)
				largest = c;
		return largest;
	}

	public static float sum(Float[] e) {
		float a = 0;
		for (Float c : e)
			a += c.floatValue();
		return a;
	}

	public static float sum(float[] e) {
		float a = 0;
		for (float z : e)
			a += z;
		return a;
	}

	public static int sum(Collection<Integer> e) {
		int a = 0;
		for (float z : e)
			a += z;
		return a;
	}

	public static int sum(int[] i) {
		int z = 0;
		if (i != null)
			for (int c : i)
				z += c;
		return z;
	}

}
